package com.autologistics.socket.service;


import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import net.sf.json.JSONObject;

import org.junit.Test;

import com.autologistics.socket.vo.AuthenticationReq;
import com.autologistics.socket.vo.MediaFileReq;
import com.autologistics.socket.vo.MediaFileRes;
import com.autologistics.socket.vo.MediaFileUploadReq;
import com.autologistics.socket.vo.MediaMetaUploadReq;
import com.autologistics.socket.vo.MediaMetaUploadRes;
import com.autologistics.socket.vo.OrderGetReq;
import com.autologistics.socket.vo.OrderWaitGetReq;
import com.autologistics.socket.vo.StateCheckRes;

public class TestIDAuthentication {

	@Test
	public void test() throws UnknownHostException, IOException {
		
		MediaFileUploadReq mediaFielUploadReq = new MediaFileUploadReq();
		mediaFielUploadReq.setChecksum(45454);
		mediaFielUploadReq.setFileName("test.avi");
		mediaFielUploadReq.setLength(4096);
		mediaFielUploadReq.setMediaID(1);
		mediaFielUploadReq.setOffset(40960);
		mediaFielUploadReq.setTotallength(56576778);
		
		System.out.println(JSONObject.fromObject(mediaFielUploadReq).toString());
		
		MediaFileReq mediaFileReq = new MediaFileReq();
		mediaFileReq.setMediaID(23);
		mediaFileReq.setStartoffset(-1);
		mediaFileReq.setEndoffset(-1);
		System.out.println("MediaFileReq:"+JSONObject.fromObject(mediaFileReq).toString());
		
		MediaFileRes mediaFielres = new MediaFileRes();
		mediaFielres.setResponseCode(0);
		mediaFielres.setResponseDescription("ok,prepare send data");
		mediaFielres.setQueueNum(0);
		System.out.println("MediaFileRes"+JSONObject.fromObject(mediaFielres).toString());
		

		MediaMetaUploadReq mediaMetaUploadReq = new MediaMetaUploadReq();
		mediaMetaUploadReq.setMediaId(1);
		mediaMetaUploadReq.setFileSequence("1,2,5,7");
		mediaMetaUploadReq.setStatrtTime("12:20:34");
		mediaMetaUploadReq.setEndTime("17:29:50");
		
		System.out.println("MediaMetaUploadReq"+JSONObject.fromObject(mediaMetaUploadReq).toString());
		
		MediaMetaUploadRes mediaMetaUploadRes  = new MediaMetaUploadRes();
		mediaMetaUploadRes.setResponseCode(0);
		mediaMetaUploadRes.setResponseDescription("ok");
		System.out.println("MediaMetaUploadRes"+JSONObject.fromObject(mediaMetaUploadRes).toString());
		
		//test media upload
		/*ServerRequest request = ServerRequest.getInstance();
		try{
			request.uploadMedia(1, 2, 0, 0);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		*/
		
	//	AuthenticationReq req = new AuthenticationReq();
		//req.setIdCardNum(124);
		//OrderWaitGetReq req = new OrderWaitGetReq();
		//req.setOrderId("0000000024");
	/*	OrderGetReq req = new OrderGetReq();
		req.setOrderNumber("0000000024");
		
		Socket so = new Socket("127.0.0.1", 50002);
		System.out.println("request:"+JSONObject.fromObject(req).toString());
		OutputStream os = so.getOutputStream();
		InputStream is = so.getInputStream();
		os.write(JSONObject.fromObject(req).toString().getBytes());
		os.flush();
		
		StringBuilder sb= new StringBuilder();
		String temp = "";
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		
		while((temp = br.readLine())!= null){
			sb.append(temp);
		}
		System.out.println("response"+sb.toString());
		br.close();
		so.close();
		
		ServerSocket ss = new ServerSocket(50000);
		Socket socket = ss.accept();
		
		InputStream is = socket.getInputStream();
		StringBuffer sb = new StringBuffer();
		
		byte[] space = new byte[1024];
		int n =0;
		while((n=is.read(space)) == 1024){
			sb.append(new String(space,0,n));
		}
		sb.append(new String(space,0,n));
		
		System.out.println(sb.toString());
		StateCheckRes res = new StateCheckRes();
		res.setTotalBlank(70);
		res.setEmpyBlank(20);
		res.setRepairingBlank(20);
		res.setUsedBlank(30);
		
		System.out.println(JSONObject.fromObject(res).toString());
		socket.getOutputStream().write(JSONObject.fromObject(res).toString().getBytes());
		ss.close();*/
	}

}
